package com.Webserver.core;

import com.Webserver.http.HttpRequest;
import com.Webserver.http.HttpResponse;
import java.io.File;
/**
 * 用于处理请求.
 */
public class DispatcherServlet {

    public void server(HttpRequest request, HttpResponse response){

        String path = request.getUri();
        File file = new File("./webapps" + path);

        //如果请求的资源存在并且是一个文件则正确.
        if (file.exists() && file.isFile()) {
            response.setEntity(file);//调用响应对象,判断
            //设置响应头
            response.putHeader("Content-Type","text/html");
            response.putHeader("Content-Length",file.length()+"");

        } else {
            response.setStatusCode(404);
            response.setStatusReason("NotFound");
            file = new File("./webapps/root/404.html");
            response.setEntity(file);
            response.putHeader("Content-Type","text/html");
            response.putHeader("Content-Length",file.length()+"");
        }

        //该响应头是告诉浏览器服务端是谁.
        response.putHeader("Server","WebServer");
    }
}
